package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Emtity.Employee;
import com.itheima.common.R;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    /**
     * 员工登录
     *
     * @param httpServletRequest
     * @param employee
     * @return
     */

    @PostMapping("login")
    public R<Employee> login(HttpServletRequest httpServletRequest, @RequestBody Employee employee) {
//        json处理数据。
//        1.将提交的密码进行md5加密处理.
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        LambdaQueryWrapper<Employee> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        /**
         *  LambdaQueryWrapper<User>    User    where    username =?  nad password   =?  这些字段都是来自于User表。
         *
         *
         */
        //        2.将页面提交的用户名查询数据库,是否存在这个用户
        objectLambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
//       结果集emp
        Employee emp = employeeService.getOne(objectLambdaQueryWrapper);
//        select * from Employee where Username=?

//        3.如果查询没有数据就是返回失败
        if (emp == null) {
//错误的参数是失败信息,成功的是data数据
            return R.error("登录失败");
        }
//        查到用户就查询密码.但是比较的是md5,说明注册的时候也是存入md5
        if (!emp.getPassword().equals(password)) {
            //失败时候
            return R.error("登录失败");
        }

//查看员工状态
        if (emp.getStatus() == 0) {
            return R.error("账号被禁用");
        }

//        运行到这里说明登录成功
//        将员工id存入seesion中.作用是什么?
        httpServletRequest.getSession().setAttribute("employee", emp.getId());

//返回找到账号的相关所有信息.包括加密密码.
        return R.success(emp);
    }

    @PostMapping("logout")
    public R<String> logout(HttpServletRequest httpServletRequest) {
        //清理session中保存员工登录的session的id属性
        httpServletRequest.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工逻辑
     *
     * @param employee
     * @return
     */
    @PostMapping()
    public R<String> save(HttpServletRequest httpServletRequest, @RequestBody Employee employee) {
        log.info("新的员工信息是:" + employee.toString());
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        long employee1 = (long) httpServletRequest.getSession().getAttribute("employee");
//        employee.setCreateUser(employee1);
//        employee.setUpdateUser(employee1);
        employeeService.save(employee);
        return R.success("添加员工成功");
    }


    /*
     * 员工信息分页查询
     *
     * */

    @GetMapping("page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("name=" + name, "page=" + page, "pageSize=" + pageSize);
//构造分页构造器,请求第page页,一页pageSize个数据.
        Page<Employee> page1 = new Page(page, pageSize);
//         构造条件构造器,where
//        泛型是用来使用字段.查询实体对应的数据库.
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();

        lambdaQueryWrapper.like(!StringUtils.isEmpty(name), Employee::getName, name);
        lambdaQueryWrapper.orderByDesc(Employee::getCreateTime);//降序
//        第一个是分页构造器  第二个是条件构造器.
        employeeService.page(page1, lambdaQueryWrapper);
        return R.success(page1);
    }

    //    根据id更新数据.
    @PutMapping()
    public R<String> update(@RequestBody Employee employee, HttpServletRequest httpServletRequest) {
        log.info(String.valueOf(employee));
        employee.setUpdateTime(LocalDateTime.now());
        Object employeeId = httpServletRequest.getSession().getAttribute("employee");

        employee.setUpdateUser((Long) employeeId);
        long id = Thread.currentThread().getId();

        log.info("线程"+id);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
//        转化后
//        createTime":"2021-05-06T17:20:07","updateTime":"2023-11-27T21:15:18"
    }

    /**
     * 查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping ("/{id}")
    public R<Employee> getById(@PathVariable String id) {
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }

}
